package com.demo8.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.demo8.basic.domain.CommonResult;
import com.demo8.system.domain.dto.SysConfigDTO;
import com.demo8.system.service.ISysConfigService;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Objects;

@RestController
@RequestMapping("system/config")
public class SysConfigController {

    @Autowired
    ISysConfigService sysConfigService;

    @SaCheckPermission(value = "system:config:list", orRole = "admin")
    @GetMapping("page")
    public CommonResult<?> page(SysConfigDTO sysConfigDTO) {
        return CommonResult.ok(sysConfigService.selectConfigPage(sysConfigDTO));
    }

    @SaCheckRole("admin")
    @SaCheckPermission(value = "system:config:list", orRole = "admin")
    @GetMapping("pageVo")
    public CommonResult<?> pageVo(SysConfigDTO sysConfigDTO) {
        return CommonResult.ok(sysConfigService.selectConfigVOPage(sysConfigDTO));
    }

    @SaCheckRole("admin")
    @SaCheckPermission(value = "system:config:list", orRole = "admin")
    @GetMapping("pageCustom")
    public CommonResult<?> pageCustom(SysConfigDTO sysConfigDTO) {
        return CommonResult.ok(sysConfigService.selectConfigVOCustomConditionPage(sysConfigDTO));
    }

    @SaCheckPermission(value = "system:config:list", orRole = "admin")
    @GetMapping("list")
    public CommonResult<?> list(SysConfigDTO sysConfigDTO) {
        return CommonResult.ok(sysConfigService.selectConfigList(sysConfigDTO));
    }

    @SaCheckPermission(value = "system:config:query", orRole = "admin")
    @GetMapping("{id}")
    public CommonResult<?> get(@PathVariable Integer id) {
        return CommonResult.ok(sysConfigService.selectOneById(id));
    }

    /**
     * 根据参数键名查询参数值
     */
    @SaCheckPermission(value = "system:config:query", orRole = "admin")
    @GetMapping(value = "/configKey/{configKey}")
    public CommonResult<?> getConfigKey(@PathVariable String configKey) {
        return CommonResult.ok(sysConfigService.selectConfigValueByKey(configKey));
    }

    @SaCheckPermission(value = "system:config:add", orRole = "admin")
    @PostMapping("add")
    public CommonResult<?> add(@Validated @RequestBody SysConfigDTO sysConfigDTO) {
        Pair<Boolean, String> booleanStringPair = sysConfigService.checkConfigKeyUnique(sysConfigDTO);
        if (!booleanStringPair.getLeft()) {
            return CommonResult.fail(booleanStringPair.getRight());
        }
//        sysConfigDTO.setCreateBy();

        return CommonResult.ok(sysConfigService.insertConfig(sysConfigDTO));
    }

    @SaCheckPermission(value = "system:config:edit", orRole = "admin")
    @PostMapping("edit")
    public CommonResult<?> edit(@Validated @RequestBody SysConfigDTO sysConfigDTO) {
        if (Objects.isNull(sysConfigDTO.getConfigId())) {
            return CommonResult.fail("参数ID不能为空");
        }
        Pair<Boolean, String> booleanStringPair = sysConfigService.checkConfigKeyUnique(sysConfigDTO);
        if (!booleanStringPair.getLeft()) {
            return CommonResult.fail(booleanStringPair.getRight());
        }
        return CommonResult.ok(sysConfigService.updateConfig(sysConfigDTO));
    }

    @SaCheckPermission(value = "system:config:remove", orRole = "admin")
    @DeleteMapping("{ids}")
    public CommonResult<?> delete(@PathVariable Long[] ids) {
        sysConfigService.deleteConfigByIds(ids);
        return CommonResult.ok();
    }

    @SaCheckPermission(value = "system:config:remove", orRole = "admin")
    @DeleteMapping("/refreshConfigCache")
    public CommonResult<?> refreshConfigCache() {
        sysConfigService.refreshConfigCache();
        return CommonResult.ok();
    }

    @SaCheckPermission(value = "system:config:export", orRole = "admin")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysConfigDTO sysConfigDTO) {
        sysConfigService.export(sysConfigDTO, response);
    }

    @SaCheckPermission(value = "system:config:export", orRole = "admin")
    @PostMapping("/exportBigData")
    public void exportBigData(HttpServletResponse response, SysConfigDTO sysConfigDTO) {
        sysConfigService.exportBigData(sysConfigDTO, response);
    }


}
